{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串'1,1,2,3,4,1,1'\n"
     ]
    }
   ],
   "source": [
    "text =input('请输入一个字符串');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "frequencies=Counter(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({',': 6, '1': 4, \"'\": 2, '2': 1, '3': 1, '4': 1})\n"
     ]
    }
   ],
   "source": [
    "print(frequencies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.输入一个字符串，输出其中每个唯一字符最后一次出现的下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个包含若干集合的列表[{1,3,5},{2,3,5}]\n"
     ]
    }
   ],
   "source": [
    "let=eval(input('请输入一个包含若干集合的列表'));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 5}\n"
     ]
    }
   ],
   "source": [
    "print(reduce(lambda x,y:x|y,let));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串\"天天年年天天\"\n"
     ]
    }
   ],
   "source": [
    "str1=input('请输入一个字符串')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "str2=str1[::-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"天天年年天天\"\n"
     ]
    }
   ],
   "source": [
    "print(str2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes\n"
     ]
    }
   ],
   "source": [
    "if str1==str2:\n",
    "    print('Yes')\n",
    "else:\n",
    "    print('No')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Python 3. x的range()函数返回一个_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "range可迭代对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 表达式“[3] in[1,2,3,4]”的值为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.列表对象的sort()方法用来对列表元素进行原地排序，该函数返回值为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.列表对象的   方法删除首次出现的指定元素,如果列表中不存在要删除的元"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "remove"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5、假设列表对象alist 的3.354511.1,15,17]，那么切片alstl 3:7得到的值是"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[6,7,9,11]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.在Python中，字典和集合都是用对    作为界定符,字典的每个元素由两部分组成，即   和  ，其中   不允许重复。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[]  键 值  键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.使用字典对象的  方法可以返回字典的“键:值”对，使用字典对象的_   方法可以返回字典的“键”，使用字典对象的  方法可以返回字典的 “值”。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "items keys()  value()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.假设有列表a=[ 'name '，' age', 'sex']和b=[ 'Dong' ,38, 'Male'],使用一条语句将这两个列表的内容转换为字典，并且以列表a中的元素为“键”以列表b中的元素为“值”，这个语句可以写为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "c=dict(zip(a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.假没有一个列表a,现要求从列表a中每3个元素取1个,并且将取到的元素组成新的列表b,可以使用语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "b=a[::3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.使用列表推导式生成包含10个数字5的列表，语句可以写为"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5 for i in range(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11._(可以、不可以)使用del命令来删除元组中的部分元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不可以"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12已知列表dna中有若干字符申，要求编写程序对dita中的字符申进行过滤，只输出重复字符不超过一一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "data = [\"www.com\", \"www.cn\", \"@www\", \"@qq.com\"] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for d in data:\n",
    "    res = Counter(d)\n",
    "    max_len = 0\n",
    "    for key in res:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if max_len < res[key]:\n",
    "    max_len = res[key]\n",
    "    n = len(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "expected an indented block (<ipython-input-3-30513d63b5d5>, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-30513d63b5d5>\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m    data.remove(d)\u001b[0m\n\u001b[1;37m                  ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
     ]
    }
   ],
   "source": [
    "if max_len > n / 2:\n",
    "    data.remove(d)\n",
    "\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.编写程序，使用筛选法查找并输出小于1000的所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.为什么应尽量从列表的尾部进行元素的增加与删除操作?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从头或者从尾部，涉及到列表元素移动，从尾部增删会拥有更高的处理速度。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.编写程序,生成包含1000个0~100的随机整数,并统计每个元素的出现次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "alist=[random.randint(0,100)for i in range(1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-9-42d92817ce23>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-9-42d92817ce23>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    print(v,':'alist.count(v))\u001b[0m\n\u001b[1;37m                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "for i in alist:\n",
    "    print(v,':'alist.count(v))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16.编写程序,用户输人一个列表和2个整数作为下标，然后输出列表中介于2个下标闭区间之间的元素组成的子列表。例如用户输人[1,2,3,4,5,6]和2,5,程序输出[3,4,5,6]。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.设计一个字典,并编写程序,用户输人内容作为“键”，然后输出字典中对应的“值\"，如果用户输人的“键”不存在，则输出“您输人的键不存在!”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dict={'a':1,'b':2,'c':3,'d':4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1000):\n",
    "    a=input(\"请输入键：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-12-753859c9ed69>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-12-753859c9ed69>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    if a_dict.get(a,'asd')==\"asd\"\u001b[0m\n\u001b[1;37m                                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "    if a_dict.get(a,'asd')==\"asd\"\n",
    "      print(\"您输入的键不存在！\")\n",
    "    else:\n",
    "        print(\"a_dict[a]\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18, 编写租作生席包含100个心00的随机根数，并统计每个元素的出2。。。。16.编写程序，用户输人一个列表和2个整数作为下标，然后输出列夷中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
